package monster.com.cvh.adapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.text.ParseException;
import java.util.List;

import monster.com.cvh.R;
import monster.com.cvh.activity.NewsDetailsActivity;
import monster.com.cvh.bean.MyCommentBean;
import monster.com.cvh.util.TimeConvert;
import monster.com.cvh.util.TimeUtil;

/**
 * @author: monster
 * @data: 2018/4/2
 * @function:我的界面：评论推送adapter
 */
public class MyCommentAdater extends BaseQuickAdapter<MyCommentBean.DataBean,BaseViewHolder> {

    private String lastTime;

    public MyCommentAdater(int layoutResId, @Nullable List<MyCommentBean.DataBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, final MyCommentBean.DataBean item) {
        //获取当前时间戳
        long timeStamp = System.currentTimeMillis() / 1000;
        try {
            String s = TimeConvert.dateToStamp(item.getComment_time());
            long articleTime = timeStamp - Long.valueOf(s) / 1000;
            lastTime = TimeUtil.calculateTime(articleTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String to_user_id = item.getTo_user_id()+"";
        if (item.getTo_user_name() == null && item.getTo_comment_content()==null){
            //为文章评论
            Glide.with(mContext).load(item.getFrom_icon_url()).into((ImageView) helper.getView(R.id.iv_item_comment_gratar));
            helper.setText(R.id.tv_item_comment_user_name,item.getFrom_user_name());
            helper.setText(R.id.tv_item_comment_time,lastTime);
            helper.setText(R.id.tv_item_comment_sender_content,item.getComment_content());
            helper.setText(R.id.item_comment_receiver_comment,"");
            helper.setText(R.id.item_comment_news_title,item.getArticle_title());
            Glide.with(mContext).load(item.getArticle_cover_image_url()).into((ImageView) helper.getView(R.id.item_comment_news_icon));
        }else {
            Glide.with(mContext).load(item.getFrom_icon_url()).into((ImageView) helper.getView(R.id.iv_item_comment_gratar));
            helper.setText(R.id.tv_item_comment_user_name,item.getFrom_user_name());
            helper.setText(R.id.tv_item_comment_time,lastTime);
            helper.setText(R.id.tv_item_comment_sender_content,item.getComment_content());

            if (String.valueOf(item.getTo_comment_id()).equals("") && item.getTo_comment_content().equals("")){
                //评论内容被干掉
                helper.setText(R.id.item_comment_receiver_comment,"@"+item.getTo_user_name()+":"+"此条评论已被删除");
            }else{
                helper.setText(R.id.item_comment_receiver_comment,"@"+item.getTo_user_name()+":"+item.getTo_comment_content());
            }
            helper.setText(R.id.item_comment_news_title,item.getArticle_title());
            Glide.with(mContext).load(item.getArticle_cover_image_url()).into((ImageView) helper.getView(R.id.item_comment_news_icon));
        }

        //跳转文章
        helper.getView(R.id.ll_item_comment_to_article).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, NewsDetailsActivity.class);
                intent.putExtra("id", item.getArticle_id());
                intent.putExtra("isCollect", item.isIs_article_collected());
                intent.putExtra("reply",item.getArticle_comment_count());
                mContext.startActivity(intent);
            }
        });
    }
}
